package com.bite.system.service.user.impl;

import cn.hutool.core.util.StrUtil;
import com.bite.common.core.constants.Constants;
import com.bite.common.core.enums.ResultCode;
import com.bite.common.security.exception.ServiceException;
import com.bite.system.domain.user.User;
import com.bite.system.domain.user.dto.UserQueryDTO;
import com.bite.system.domain.user.dto.UserUpdateStatusDTO;
import com.bite.system.domain.user.vo.UserVO;
import com.bite.system.manager.UserCacheManager;
import com.bite.system.mapper.user.UserMapper;
import com.bite.system.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 17450
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-05-24 14:25:54
*/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserCacheManager userCacheManager;

    @Override
    public List<UserVO> list(UserQueryDTO userQueryDTO) {
        String questionIdsStr = userQueryDTO.getExcludeQuestionIdsStr();
        if (StrUtil.isNotEmpty(questionIdsStr)) {
            String[] splitIdsStr = questionIdsStr.split(Constants.SPILT_SEM);
            Set<Long> splitIds = Arrays.stream(splitIdsStr).map(Long::parseLong).collect(Collectors.toSet());
            userQueryDTO.setExcludeUserIdSet(splitIds);
        }

        return userMapper.list(userQueryDTO);
    }

    @Override
    public int updateStatus(UserUpdateStatusDTO userUpdateDTO) {
        User user = userMapper.selectById(userUpdateDTO.getUserId());
        if (user == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(userUpdateDTO.getStatus());
        // 刷新缓存（如果存在）
        userCacheManager.refreshUser(user);
        return userMapper.updateById(user);
    }


}




